package com.example.dbdesign.controller;

import com.example.dbdesign.pojo.vo.Res;
import com.example.dbdesign.service.FilePathService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
@Slf4j
@RestController
public class FilePathController {

    @Autowired
    private FilePathService filePathService;

    @Value("${upload.UploadPath}")
    private String uploadPath;

    @Value("${upload.AccessPath}")
    private String accessPath;

    @PostMapping("/upload")
    public Res upload(HttpServletRequest request) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");
        String fileName = System.currentTimeMillis() + file.getOriginalFilename();
        String filePath = uploadPath + fileName;
        log.info("FileName:{}", fileName);
        filePathService.save(file, filePath);

        return Res.success(accessPath + fileName);
    }
}
